package util.groovy.command


import rowingmanager.domain.Address
import rowingmanager.domain.Rower
import rowingmanager.domain.Country
import org.jsecurity.crypto.hash.Md5Hash
import org.apache.log4j.Logger
import rowingmanager.domain.RowerProfile


/**
 * Created by IntelliJ IDEA.
 * User: Simon
 * Date: 29.9.2009
 * Time: 23:59:56
 * To change this template use File | Settings | File Templates.
 */

public class RowerCreateCommand {


  String name
  String surname
  Date birthday
  String phone
  String email
  boolean sex //true = male , false = female
  String password

  String street
  String state
  String city
  String postalCode

  int weight
  int height
  String shoeSize


  static constraints = {
   name(nullable:false,maxSize:30)
   surname(nullable:false,maxSize:30)
   //birthday(min:new Date("1.1.1910"),max:new Date().minus(8*365) ) //recmo da je najmlajsi clan star 8 let
   phone(maxSize:25)
   email(email:true,maxSize:50,blank:false)  //unique constraint can not be applied via command object
   password(blank:false,maxSize:20,minSize:5)
   street(blank:false,nullable:false,maxSize:40,minSize:5)
   state(blank:true,maxSize:20)
   city(nullable:false,maxSize:20)
   postalCode(nullable:false,maxSize:10)
   weight(min:30,max:180)
   height(min:100,max:280)
   shoeSize(maxSize:10)

  }

  Rower createRower(String countryParam){
    def rower = new Rower(name:name,surname:surname,
                          birthday:birthday,phone:phone,
                          email:email,sex:sex)
    def address = new Address(street:street,state:state,city:city,postalCode:postalCode)
   // log.info("country param is ${countryParam} ")
    address.country = Country.get(countryParam)
    rower.address = address
    rower.password = new Md5Hash(password).toHex()
    def profile = new RowerProfile(weight:weight,height:height,shoeSize:shoeSize)
    rower.profile = profile;

    return rower
  }
}